﻿using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Practices.RecipeFramework;
using VSSDKAssist.Assets.Actions.Base;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.Common.Actions
{
    public class CreateBitmapStripAction : ActionBase
    {
        public const int ImageSize = 16;

        [Input(Required=true)]
        public List<string> IconList { get; set; }

        [Output]
        public Bitmap Bitmap { get; set; }

        protected override void OnExecute()
        {
            this.Bitmap = CreateBitmapStrip(GetImageList(this.IconList));
        }

        protected override void OnUndo()
        {
            //Do Nothing
        }

        public static Bitmap CreateBitmapStrip(ImageList imageList)
        {
            if(imageList.Images.Count > 0)
            {
                Bitmap bitmap = new Bitmap(imageList.Images.Count * ImageSize, ImageSize);
                using(Graphics g = Graphics.FromImage(bitmap))
                {
                    int x = 0;
                    foreach(Image image in imageList.Images)
                    {
                        g.DrawImage(image, x, 0);
                        x += ImageSize;
                    }
                }

                bitmap.MakeTransparent(Color.Magenta);
                return bitmap;
            }

            return null;
        }

        private static ImageList GetImageList(IEnumerable<string> iconFileNames)
        {
            ImageList imageList = new ImageList();

            imageList.ColorDepth = ColorDepth.Depth32Bit;
            imageList.ImageSize = new Size(ImageSize, ImageSize);

            foreach(string iconFileName in iconFileNames)
            {
                ImagelistHelper.AddFromIconFile(iconFileName, imageList);
            }

            imageList.TransparentColor = Color.Magenta;

            return imageList;
        }
    }
}